package com.crl.module.eventParser
{
	import com.crl.core.IDispose;
	import com.crl.utils.HashMap;
	
	import flash.events.Event;
	import flash.events.EventDispatcher;
	import flash.events.IEventDispatcher;
	
	/**
	 * 事件解析工厂; 
	 * @author crl
	 * 
	 */	
	public class EventParserFactory implements IEventParserFactory ,IEventDispatcher,IDispose
	{
		private var map:HashMap;
		private var eventDispatcher:EventDispatcher;
		public function EventParserFactory()
		{
			eventDispatcher=new EventDispatcher(this);
			getMap();
		}
		
		private function getMap():HashMap{
			if(map==null){
				map=new HashMap();
			}
			return map;
		}
		
		
		
		/// Registers an event listener object with an EventDispatcher object so that the listener receives notification of an event.
		public function addEventListener (type:String, listener:Function, useCapture:Boolean = false, priority:int = 0, useWeakReference:Boolean = false) : void{
			this.eventDispatcher.addEventListener(type,listener,useCapture,priority,useWeakReference);
		}
		
		/// Dispatches an event into the event flow.
		public function dispatchEvent (event:Event) : Boolean{
			return this.eventDispatcher.dispatchEvent(event);
		}
		
		/// Checks whether the EventDispatcher object has any listeners registered for a specific type of event.
		public function hasEventListener (type:String) : Boolean{
			return this.eventDispatcher.hasEventListener(type);
		}
		
		/// Removes a listener from the EventDispatcher object.
		public function removeEventListener (type:String, listener:Function, useCapture:Boolean = false) : void{
			return this.eventDispatcher.removeEventListener(type,listener,useCapture);
		}
		
		/// Checks whether an event listener is registered with this EventDispatcher object or any of its ancestors for the specified event type.
		public function willTrigger (type:String) : Boolean{
			return this.eventDispatcher.willTrigger(type);
		}
		
		
		
		
		/**
		 * 添加事件解析器; 
		 * @param parser
		 * @param useCapture
		 * @param priority
		 * @param useWeakreference
		 * @return 
		 * 
		 */		
		public function addParser(parser:IEventParser,useCapture:Boolean=false,priority:int=0,useWeakreference:Boolean=true):Boolean{
			var type:String=parser.getType();
			
			if(map.containsKey(type)){
				trace("处理器已经存在,如果你的是新的想覆盖旧的,请使用getParser与removeParser删掉旧版的")
				return false;
			}
			var list:Vector.<String>=parser.getEvents();
			
			for each(var eventStr:String in list){
				this.addEventListener(eventStr,parser.toParse,useCapture,priority,useWeakreference);
			}
			parser.factory=this;
			map.put(type,parser);
			return true;
		}
		
		/**
		 * 删除一个事件解析器; 
		 * @param parser
		 * @param useCapture
		 * @return 
		 * 
		 */		
		public function removeParser(parser:IEventParser,useCapture:Boolean=false):Boolean{
			var type:String=parser.getType();
			if(map.containsKey(type)==false){
				return false;
			}
			
			var list:Vector.<String>=parser.getEvents();
			
			for each(var eventStr:String in list){
				this.removeEventListener(eventStr,parser.toParse,useCapture);
			}
			parser.factory=null;
			map.remove(type);
			return true;
		}
		
		/**
		 * 由类型来删除事件解析器; 
		 * @param type
		 * @param useCapture
		 * @return 
		 * 
		 */		
		public function getParser(type:String):IEventParser{
			if(this.map.containsKey(type)==false){
				return null;
			}
			var parser:IEventParser=this.map.getValue(type);
			return parser;
		}
		
		/**
		 * 清理所有; 
		 * 
		 */		
		public function clear():void{
			/*var parsers:Array=this.map.values();
			for each(var parser:IEventParser in parsers){
				this.removeParser(parser);
				//todo removeParser if useCapture=true;
			}*/
			//抛弃原来的eventDispatcher会清理了所有的旧事件侦听的引用;
			this.eventDispatcher=new EventDispatcher(this);
			this.map.clear();
		}
		
		public function dispose():void{
			this.map=null;
			this.eventDispatcher=null;
			//todo clear other;
		}
		
	}
}